package com.supercube.sport.yoga.service.impl;

import com.github.pagehelper.PageHelper;
import com.supercube.common.utils.TimeUtils;
import com.supercube.sport.yoga.bo.MemberBO;
import com.supercube.sport.yoga.dao.BookingCourseMapper;
import com.supercube.sport.yoga.dao.HomeMapper;
import com.supercube.sport.yoga.dao.MemberBodyInfoMapper;
import com.supercube.sport.yoga.dao.MemberConsumeRecordMapper;
import com.supercube.sport.yoga.dao.MemberIntegralRecordMapper;
import com.supercube.sport.yoga.dao.MemberMapper;
import com.supercube.sport.yoga.domain.CardGroupByMemberDTO;
import com.supercube.sport.yoga.domain.MemberBodyInfoDTO;
import com.supercube.sport.yoga.domain.MemberDTO;
import com.supercube.sport.yoga.domain.MemberIntegralRecordDTO;
import com.supercube.sport.yoga.service.MemberService;
import com.supercube.sport.yoga.vo.BookingCourseBo;
import com.supercube.sport.yoga.vo.MemberAndCardVo;
import com.supercube.sport.yoga.vo.MemberConsumeRecordVo;
import com.supercube.sport.yoga.vo.MemberVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private MemberConsumeRecordMapper memberConsumeRecordMapper;
    @Autowired
    private MemberBodyInfoMapper memberBodyInfoMapper;
    @Autowired
    private MemberIntegralRecordMapper memberIntegralRecordMapper;
    @Autowired
    private BookingCourseMapper bookingCourseMapper;
    @Autowired
    private HomeMapper homeMapper;

    @Override
    public Integer save(MemberDTO memberDTO) {
        return memberMapper.insert(memberDTO);
    }

    @Override
    public Integer update(MemberDTO memberDTO) {
        return memberMapper.updateByPrimaryKeySelective(memberDTO);
    }

    @Override
    public Integer deleteMember(String id) {
        return memberMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Integer batchDeleteMember(List<String> ids) {
        return memberMapper.batchDeleteMember(ids);
    }

    public MemberVO memberInfo(String id) {
        return memberMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<MemberVO> selectMemberList(MemberBO bo) {
        //1:未上课、2：会员到期、3：会员生日、4：即将失效会员数、5:未绑卡会员
        String operType = String.valueOf(bo.getOperType());
        Integer days = 0;
        String d = String.valueOf(bo.getDays());
        if (!Objects.equals("null", d)) {
            days = Integer.valueOf(d);
        }
        String type = String.valueOf(bo.getRange());
        List<String> ids = null;
        if (Objects.equals("1", operType)) {
            ids = homeMapper.getFutureClassesCount(days);
        } else if (Objects.equals("2", operType)) {
            ids = homeMapper.getMemberExpirationCount(days);
        } else if (Objects.equals("3", operType)) {
            ids = homeMapper.getMemberBirthdayCount(days);
        } else if (Objects.equals("4", operType)) {
            String currentTime = TimeUtils.getCurrentTime();
            String sDate = currentTime;
            if (Objects.equals(type, "day")) {
                sDate = currentTime.substring(0, 10);
            } else if (Objects.equals(type, "month")) {
                sDate = currentTime.substring(0, 7);
            } else if (Objects.equals(type, "year")) {
                sDate = currentTime.substring(0, 4);
            }
            ids = homeMapper.getMemberInvalidCount(type, sDate);
        } else if (Objects.equals("5", operType)) {
            ids = homeMapper.getNotCardCount();
        }
        bo.setIds(ids);
        PageHelper.startPage(bo.getPageNum(), bo.getPageSize());
        List<MemberVO> list = memberMapper.selectMemberList(bo);
        List<String> collect = list.stream().map(MemberVO::getId).collect(Collectors.toList());
        if(collect.size()>0){
            List<CardGroupByMemberDTO> cardList = memberMapper.selectAllCardGroupByMember(collect);
            Map<String, List<CardGroupByMemberDTO>> listMap = cardList.stream()
                    .collect(Collectors.groupingBy(CardGroupByMemberDTO::getMemberId));
            list.forEach(memberVO -> memberVO.setCardGroupByMemberDTOList(listMap.get(memberVO.getId())));
        }
        return list;
    }

    public List<MemberVO> selectMember(Map<String, Object> map) {
        List<MemberVO> list = memberMapper.selectMemberListByLike(map);
        return list;
    }

    public String queryPhone(String tel) {
        return memberMapper.queryPhone(tel);
    }

    public List<MemberConsumeRecordVo> getConsumeRecord(String memberId, String cardId) {
        return memberConsumeRecordMapper.selectByMemberId(memberId, cardId);
    }

    public List<MemberBodyInfoDTO> getMemberBodyInfo(String memberId) {
        return memberBodyInfoMapper.getMemberBodyInfo(memberId);
    }

    public List<MemberBodyInfoDTO> queryMemberBodyInfoForFile(String memberId, String bodyExaminationTime) {
        return memberBodyInfoMapper.selectMemberBodyInfoForFile(memberId, bodyExaminationTime);
    }

    public List<MemberIntegralRecordDTO> getMemberIntegral(String memberId) {
        return memberIntegralRecordMapper.getMemberIntegral(memberId);
    }

    public List<BookingCourseBo> getBookingCourse(String memberId) {
        return bookingCourseMapper.getBookingCourse(memberId);
    }

    public List<MemberAndCardVo> selectMemberAndCard() {
        return bookingCourseMapper.selectMemberAndCard();
    }

    public List<MemberVO> getMemberByVenueId(String venueId) {
        return memberMapper.getMemberByVenueId(venueId);
    }
}
